package com.rjkj.medicalwaste.utils;



import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class Encoder03Small implements Encode03IF {
    private String frameHeader = "AA5503";
    private String frameFooter = "99BB66";

    public String getLength() {
        String len = "0000" + getContext().length;
        return len.substring(len.length() - 4);
    }

    private String QRcode;
    private String QRcodeType;
    private byte[] infobytes;
    private String garbageType;
    private String hospitalID;
    public void setQRcodeTest(String QRcodeType, String macAddress,String hospitalID) {
        this.hospitalID = hospitalID;
        String QRcodeStart = "1A31000303";
        String QRcodeX = "0500";
        String QRcodeY = "2600";
        String QRcodeRange = "0400";
        if ("2".equals(QRcodeType)) {
            QRcodeY = "3600";
        }

        String QRcodeEnd = "00";
        this.QRcode = QRcodeStart + QRcodeX + QRcodeY + QRcodeRange +
                SerializeUtil.byteArrayToHexString(macAddress.getBytes()) + QRcodeEnd;
        this.QRcodeType = QRcodeType;
    }
    public void setQRcode(String QRcodeType, String macAddress,String hospitalID) {
        this.hospitalID = hospitalID;
        String QRcodeStart = "1A31000303";
        String QRcodeX = "0500";
        String QRcodeY = "2600";
        String QRcodeRange = "0400";
        if ("2".equals(QRcodeType)) {
            QRcodeY = "3600";
        }
        String QRcodeContext = "https://zjrongjia.com/QRTrace/?" + macAddress;

        String QRcodeEnd = "00";
        this.QRcode = QRcodeStart + QRcodeX + QRcodeY + QRcodeRange +
                SerializeUtil.byteArrayToHexString(QRcodeContext.getBytes()) + QRcodeEnd;
        this.QRcodeType = QRcodeType;
    }

    public String getQRcode() {
        return QRcode.isEmpty() ? "" : QRcode;
    }

    private String info;

    public void setGarbage(String hospital, String department, String weight, String type, String cleaner, String nurse, String timestamp) {
        this.garbageType = type;
        String templete = "1A5400xxxxyyyyzzzz00";
        byte[] templetearray = new byte[]{0x1a, 0x54, 0x00};
        try {
            //医院
            byte[] hospitalbytes = null;
            byte[] typebytes = null;

            //医院
            String printHospital = null;
            int X_;
            if (hospital.length() > 8) {
                X_ = 0x40;
            } else {
                X_ = 0x90;
            }
            int X2_;
            if (QRcodeType.equals("1") ) {
                X2_ = 0xA0;
            } else {
                X2_ = 0x90;
            }
            if (!QRcodeType.equals("2")) {
                if (hospitalID.substring(0, 4).equals("0558") && (garbageType.equals("感染性") || garbageType.equals("药物性") || garbageType.equals("病理性"))) {
                    X2_ = 0x50;
                }
            }
            if ("1".equals(QRcodeType)) {
                hospitalbytes = ItemBytes(hospital, new byte[]{(byte) X_, 0x00, 0x08, 0x00});
            } else if ("2".equals(QRcodeType)) {
                hospitalbytes = ItemBytes(hospital, new byte[]{(byte) X_, 0x00, 0x28, 0x00});
            } else {
                hospitalbytes = ItemBytes(hospital, new byte[]{(byte) X_, 0x00, 0x28, 0x00});
            }
            //科室
            byte[] departmentbytes = ItemBytes("科室：" + department, new byte[]{(byte) X2_, 0x00, 0x28, 0x00});
            //重量
//            byte[] weightbytes = ItemBytes("重量：" + weight, new byte[]{(byte) 0x90, 0x00, 0x48, 0x00});
            //种类
            if ("1".equals(QRcodeType)) {
                typebytes = ItemBytes(type + "：" + weight, new byte[]{(byte) X2_, 0x00, (byte) 0x48, 0x00});
            } else if ("2".equals(QRcodeType)) {
                if (type.equals("")) {
                    typebytes = ItemBytes("医疗废物箱", new byte[]{(byte) X2_, 0x00, (byte) 0x68, 0x00});
                } else {
                    typebytes = ItemBytes("种类：" + type, new byte[]{(byte) X2_, 0x00, (byte) 0x68, 0x00});
                }
            } else {
                typebytes = ItemBytes(type, new byte[]{(byte) 0x90, 0x00, (byte) 0x68, 0x00});
            }

            //清理工--收集人
            byte[] cleanerbytes = ItemBytes("收集人：" + cleaner, new byte[]{(byte) X2_, 0x00, (byte) 0x68, 0x00});
            //护士--确认人
            byte[] nursebytes = ItemBytes("确认人：" + nurse, new byte[]{(byte) X2_, 0x00, (byte) 0x88, 0x00});
            //日期 格式：18-12-07 14:02:56
            String printTimestamp = null;
            byte[] timebytes = null;
            if (timestamp.length() > 5) {
                timestamp = timestamp.substring(2);
            } else {
                timestamp = "未知";
            }
            timebytes = ItemBytes(timestamp, new byte[]{(byte) X2_, 0x00, (byte) 0xA8, 0x00});

            if ("1".equals(QRcodeType)) {
//                infobytes = combineArrays(hospitalbytes, departmentbytes, weightbytes, typebytes, cleanerbytes, nursebytes, timebytes);
                infobytes = combineArrays(hospitalbytes, departmentbytes, typebytes, cleanerbytes, nursebytes, timebytes);
            } else if ("2".equals(QRcodeType)) {
                infobytes = combineArrays(hospitalbytes, typebytes, timebytes);
            } else {
                infobytes = combineArrays(hospitalbytes, typebytes, timebytes);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void setTestGarbage(String hospital, String department, String weight, String type, String cleaner, String nurse, String timestamp) {
        this.garbageType = type;
        try {
            //医院
            byte[] hospitalbytes = null;
            byte[] typebytes = null;

            //医院
            String printHospital = null;
            int X_;
            if (hospital.length() > 8) {
                X_ = 0x40;
            } else {
                X_ = 0x90;
            }
            int X2_;
            if (QRcodeType.equals("1") ) {
                X2_ = 0xA0;
            } else {
                X2_ = 0x90;
            }
            if (!QRcodeType.equals("2")) {
                if (hospitalID.substring(0, 4).equals("0558") && (garbageType.equals("感染性") || garbageType.equals("药物性") || garbageType.equals("病理性"))) {
                    X2_ = 0x50;
                }
            }
            if ("1".equals(QRcodeType)) {
                hospitalbytes = ItemBytes(hospital, new byte[]{(byte) X_, 0x00, 0x08, 0x00});
            } else if ("2".equals(QRcodeType)) {
                hospitalbytes = ItemBytes(hospital, new byte[]{(byte) X_, 0x00, 0x28, 0x00});
            } else {
                hospitalbytes = ItemBytes(hospital, new byte[]{(byte) X_, 0x00, 0x28, 0x00});
            }
            //科室
            byte[] departmentbytes = ItemBytes("" + department, new byte[]{(byte) X2_, 0x00, 0x28, 0x00});
            //重量
//            byte[] weightbytes = ItemBytes("重量：" + weight, new byte[]{(byte) 0x90, 0x00, 0x48, 0x00});
            //种类
            if ("1".equals(QRcodeType)) {
                typebytes = ItemBytes(type + "序号:" + weight, new byte[]{(byte) X2_, 0x00, (byte) 0x48, 0x00});
            } else if ("2".equals(QRcodeType)) {
                if (type.equals("")) {
                    typebytes = ItemBytes("", new byte[]{(byte) X2_, 0x00, (byte) 0x68, 0x00});
                } else {
                    typebytes = ItemBytes("" + type, new byte[]{(byte) X2_, 0x00, (byte) 0x68, 0x00});
                }
            } else {
                typebytes = ItemBytes(type, new byte[]{(byte) 0x90, 0x00, (byte) 0x68, 0x00});
            }

            //清理工--收集人
            byte[] cleanerbytes = ItemBytes("" + cleaner, new byte[]{(byte) X2_, 0x00, (byte) 0x68, 0x00});
            //护士--确认人
            byte[] nursebytes = ItemBytes("" + nurse, new byte[]{(byte) X2_, 0x00, (byte) 0x88, 0x00});
            //日期 格式：18-12-07 14:02:56
            String printTimestamp = null;
            byte[] timebytes = null;
            if (timestamp.length() > 5) {
                timestamp = timestamp.substring(2);
            } else {
                timestamp = "";
            }
            timebytes = ItemBytes(timestamp, new byte[]{(byte) X2_, 0x00, (byte) 0xA8, 0x00});

            if ("1".equals(QRcodeType)) {
//                infobytes = combineArrays(hospitalbytes, departmentbytes, weightbytes, typebytes, cleanerbytes, nursebytes, timebytes);
                infobytes = combineArrays(hospitalbytes, departmentbytes, typebytes, cleanerbytes, nursebytes, timebytes);
            } else if ("2".equals(QRcodeType)) {
                infobytes = combineArrays(hospitalbytes, typebytes, timebytes);
            } else {
                infobytes = combineArrays(hospitalbytes, typebytes, timebytes);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*
    public  String getContext(){
        String templete="01 01 1A 5B 01 00 00 00 00 80 01 F0 00 00 QRcodeinfo1A5D001A4F00";
        return templete.replace("QRcode",QRcode).replace("info",info);
    }*/

    public byte[] getContext() {
        byte[] headerbytes = new byte[]{0x01, 0x01, 0x1A, 0x5B, 0x01, 0x00, 0x00, 0x00, 0x00, (byte) 0x80, 0x01, (byte) 0xF0, 0x00, 0x00};
        byte[] endbytes = new byte[]{0x1A, 0x5D, 0x00, 0x1A, 0x4F, 0x00, 0x1B, 0x69};
        byte[] qrcodebytes = SerializeUtil.hexStringToByteArray(QRcode);
        byte[] printData;
        if (!QRcodeType.equals("2")) {
            if (hospitalID.substring(0, 4).equals("0558") && (garbageType.equals("感染性") || garbageType.equals("药物性") || garbageType.equals("病理性"))) {
                printData = combineArrays(headerbytes, infobytes, endbytes);
            } else {
                printData = combineArrays(headerbytes, qrcodebytes, infobytes, endbytes);
            }

//            //双份打印
//            if (SPUtils.getSharedBooleanData(MyApplication.getInstance(), ParamsConstant.SYSTEM_DOUBLE_COLLECT_PAPER)) {
//                printData = mergeBytes(printData, printData);
//            }
        } else {
            printData = combineArrays(headerbytes, qrcodebytes, infobytes, endbytes);
        }
        return printData;
    }

    /**
     * 将两个byte数组合并为一个
     *
     * @param data1 要合并的数组1
     * @param data2 要合并的数组2
     * @return 合并后的新数组
     */
    public byte[] mergeBytes(byte[] data1, byte[] data2) {
        byte[] data3 = new byte[data1.length + data2.length];
        System.arraycopy(data1, 0, data3, 0, data1.length);
        System.arraycopy(data2, 0, data3, data1.length, data2.length);
        return data3;
    }

    public byte[] getPirntMessage() {
        byte[] printbytes = getContext();
        int length = printbytes.length;
        byte[] headerbytes = new byte[]{(byte) 0xaa, 0x55, 0x03, (byte) (length / 256), (byte) (length % 256)};
        int sum = 0;
        byte[] temp = combineArrays(headerbytes, printbytes);
        for (byte b : temp) {
            sum += b;

        }
        byte[] endbytes = new byte[]{(byte) (sum % 256), (byte) 0xbb, 0x66};

        return combineArrays(temp, endbytes);
    }

    public String toString() {
        return frameHeader + getLength() + Arrays.toString(getContext()) + frameFooter;
    }


    private byte[] ItemBytes(String gbk, byte[] data) {
        byte[] stringbytes = GBK2Bytes(gbk);
        return combineArrays(new byte[]{0x1a, 0x54, 0x00}, data, stringbytes, new byte[]{0x00});
    }

    private byte[] GBK2Bytes(String gbk) {
        if (gbk != null) {
            try {
                return gbk.getBytes("gb2312");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public byte[] combineArrays(byte[]... info) {
        int length = 0;
        for (byte[] arr : info) {
            length += arr.length;
        }
        byte[] result = new byte[length];
        int index = 0;
        for (byte[] str : info) {
            System.arraycopy(str, 0, result, index, str.length);
            index += str.length;
        }
        return result;
    }
}
